package org.rcy.ruicingmarket.goods;

import org.rcy.framework.api.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/12/19 15:12
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsService {
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private GoodsDetailDao goodsDetailDao;

	public Long addGoods(GoodsVo vo){
		Goods goods = convertToPo(vo);
		goods.setCreateTime(new Date());
		Goods save1 = goodsDao.save(goods);
		GoodsDetail goodsDetail = goods.getGoodsDetail();
		if(save1.getId() != null){
			goodsDetail.setGoodsId(save1.getId());
			goodsDetail.setCreateTime(new Date());
			goodsDetailDao.save(goodsDetail);
		}
		return save1.getId();
	}

	public PageResult<GoodsVo> pageQuery(GoodsPageQueryRequest request){
       if(request.getMonthlySalesMax() != null || request.getMonthlySalesMin() != null || request.getStock() != null) {
		   List<Long> ids = goodsDetailDao.getGoodsIdsByCondition(request);
		   request.setIds(ids);
	   }
	   int count = goodsDao.countCondition(request);
		List<Goods> goodsList = goodsDao.pageQuery(request);
		List<GoodsVo> vos = new ArrayList<>();
		for (Goods goods : goodsList) {
			GoodsDetail goodsDetail = goodsDetailDao.getByGoodsId(goods.getId());
			goods.setGoodsDetail(goodsDetail);
			GoodsVo goodsVo = convertToVo(goods);
			vos.add(goodsVo);
		}
		return new PageResult<>(request.getPageNo(),request.getPageSize(),count,vos);
	}

	public int countByCondition(GoodsPageQueryRequest request){
		return goodsDao.countCondition(request);
	}

	public void updateGoods(GoodsVo vo){
		Goods goods = convertToPo(vo);
		goodsDao.update(goods,true);
		GoodsDetail byGoodsId = goodsDetailDao.getByGoodsId(goods.getId());
		if(byGoodsId != null){
			goods.getGoodsDetail().setId(byGoodsId.getId());
			goodsDetailDao.update(goods.getGoodsDetail(),true);
		}
	}

	public GoodsVo getGoodsByName(String goodsName){
		Goods goods = goodsDao.getByName(goodsName);
		if(goods != null){
			GoodsDetail goodsDetail = goodsDetailDao.getByGoodsId(goods.getId());
			goods.setGoodsDetail(goodsDetail);
		}
		return convertToVo(goods);
	}

	public GoodsVo findById(Long id){
		Goods goods = goodsDao.get(id);
		GoodsDetail goodsDetail = goodsDetailDao.getByGoodsId(id);
		if(goods != null) {
			goods.setGoodsDetail(goodsDetail);
		}
		return convertToVo(goods);
	}

	public void deleteById(Long id){
		goodsDao.delete(id);
		goodsDetailDao.deleteByGoodsId(id);
	}

	private Goods convertToPo(GoodsVo vo){
		Goods goods = new Goods();
		GoodsDetail goodsDetail = new GoodsDetail();
		goods.setId(vo.getGid());
		goods.setName(vo.getName());
		goods.setCid(vo.getCid());
		goods.setPrice_max(vo.getPrice_max());
		goods.setPrice_min(vo.getPrice_min());
		goods.setAid(vo.getAid());
		goods.setSellerId(vo.getSellerId());
		goods.setImg(vo.getImg());
		goods.setStatus(vo.getStatus());

		goodsDetail.setGoodsId(vo.getGid());
		goodsDetail.setDescription(vo.getDescription());
		goodsDetail.setDetailImg1(vo.getDetailImg1());
		goodsDetail.setDetailImg2(vo.getDetailImg2());
		goodsDetail.setDetailImg3(vo.getDetailImg3());
		goodsDetail.setDetailImg4(vo.getDetailImg4());
		goodsDetail.setAttrs(vo.getAttrs());
		goodsDetail.setFeatureInfo(vo.getFeatureInfo());
		goodsDetail.setCommentcount(vo.getCommentcount());
		goodsDetail.setMonthlySales(vo.getMonthlySales());
		goodsDetail.setPrice(vo.getPrice());
		goodsDetail.setStock(vo.getStock());

		goods.setGoodsDetail(goodsDetail);
		return goods;
	}

	private GoodsVo convertToVo(Goods goods){
		if(goods == null ) {
			return null;
		}
		GoodsVo goodsVo = new GoodsVo();
		GoodsDetail goodsDetail = goods.getGoodsDetail();
		goodsVo.setAid(goods.getAid());
		goodsVo.setGid(goods.getId());
		goodsVo.setName(goods.getName());
		goodsVo.setCid(goods.getCid());
		goodsVo.setPrice_max(goods.getPrice_max());
		goodsVo.setPrice_min(goods.getPrice_min());
		goodsVo.setSellerId(goods.getSellerId());
		goodsVo.setImg(goods.getImg());
		goodsVo.setStatus(goods.getStatus());

		if(goodsDetail != null){
			goodsVo.setDescription(goodsDetail.getDescription());
			goodsVo.setDetailImg1(goodsDetail.getDetailImg1());
			goodsVo.setDetailImg2(goodsDetail.getDetailImg2());
			goodsVo.setDetailImg3(goodsDetail.getDetailImg3());
			goodsVo.setDetailImg4(goodsDetail.getDetailImg4());
			goodsVo.setAttrs(goodsDetail.getAttrs());
			goodsVo.setFeatureInfo(goodsDetail.getFeatureInfo());
			goodsVo.setCommentcount(goodsDetail.getCommentcount());
			goodsVo.setMonthlySales(goodsDetail.getMonthlySales());
			goodsVo.setPrice(goodsDetail.getPrice());
			goodsVo.setStock(goodsDetail.getStock());
		}
		return goodsVo;
	}
}
